package com.tensquare.article.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Slf4j
@CrossOrigin  //解决跨域注解
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private HttpServletRequest request;

    private static final Logger LOGGER = LoggerFactory.getLogger(ArticleController.class);


    /**
    * Created by : 大萝卜
    * Date: 2020-4-4        测试Exception
    */
    @RequestMapping(value = "exception",method = RequestMethod.GET)
    public Result exception(){
        int a = 1/0;
        return null;
    }

    /**
    * Created by : 大萝卜
    * Date: 2020-4-4        分页数据
    */
    @RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map){
        //条件
        Page<Article> pageData = this.articleService.findByPage(page,size,map);
        //封装
        PageResult<Article> pageResult = new PageResult<>(
                pageData.getTotal(),pageData.getRecords()
        );
        if (pageResult == null){
            log.info("pageResult为空：{}",pageResult);
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        //数据
        log.info("请求分页成功！{}",this.request.getRequestURL());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


    /**
    * Created by : 大萝卜
    * Date: 2020-4-3      delete
    */
    @RequestMapping(value = "{articleId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("articleId") String articleId){
        if (StringUtils.isBlank(articleId)){
            log.info("请求层：{},方法：{},参数为空：{}",this.getClass().getName(),this.request.getMethod(),articleId);
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        this.articleService.deleteById(articleId);
        System.out.println(this.getClass().getName());
        log.info("删除成功!,请求方法：{}",this.request.getMethod());
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
    * Created by : 大萝卜
    * Date: 2020-4-3        update
    */
    @RequestMapping(value = "{articleId}",method = RequestMethod.PUT)
    public Result updateById(@PathVariable("articleId") String articleId,@RequestBody Article article){
        article.setId(articleId);
        this.articleService.updateById(article);
        System.out.println(this.getClass().getName());
        log.info("修改成功！，方法：{}",this.getClass().getName());
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /**
    * Created by : 大萝卜
    * Date: 2020-3-31       save
    */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        this.articleService.save(article);
        log.info("请求成功！,ip:{}",this.request.getRemoteAddr());
        return new Result(true,StatusCode.OK,"新增成功");
    }

    /**
    * Created by : 大萝卜
    * Date: 2020-3-31       id
    */
    @RequestMapping(value = "{articleId}",method = RequestMethod.GET)
    public Result findArticleById(@PathVariable("articleId") String articleId){
        if (StringUtils.isBlank(articleId)){
            log.info("方法：{} ,失败参数信息:{}",this.getClass().getName(),articleId);
            return new Result(false, StatusCode.ERROR,"参数错误");
        }
        Article article = this.articleService.findArticleById(articleId);
        if (article == null){
            log.info("方法：{} ,失败参数信息:{}",this.getClass().getName(),article.toString());
            return new Result(false, StatusCode.ERROR,"查询为空");
        }
        log.info("请求成功！,ip:{}",this.request.getRemoteAddr());
        return new Result(true,StatusCode.OK,"查询成功",article);
    }



    /**
    * Created by : 大萝卜
    * Date: 2020-3-31       文章全部列表
    */
    @RequestMapping(method = RequestMethod.GET)
    public Result getArticle(){
        List<Article> articles = this.articleService.findAll();
        if (CollectionUtils.isEmpty(articles)){
            log.info("方法：{} ,失败参数信息:{}",this.getClass().getName(),articles);
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
        log.info("请求成功！,ip:{}",this.request.getRemoteAddr());
        return new Result(true, StatusCode.OK,"查询成功",articles);
    }
}
